/*
' Copyright (c) 2011  Blue Age Software
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Common;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;


namespace BAS.AutoLogin {

    /// <summary>
    /// If user is not loged in module try automaticly login user
    /// </summary>
    public partial class View : PortalModuleBase {

        override protected void OnInit(EventArgs e) {
            this.Load += new System.EventHandler(this.Page_Load);
            base.OnInit(e);
        }

        private void Page_Load(object sender, System.EventArgs e) {
            try {
                if (!IsPostBack 
                    && UserId == -1 
                    && Settings["AutoLogin_UserName"] != null
                    && Settings["AutoLogin_UserName"].ToString().Length > 0
                    && Settings["AutoLogin_Password"] != null
                    && Settings["AutoLogin_Password"].ToString().Length > 0
                    && (Request.UrlReferrer==null || Globals.NavigateURL(TabId) != Request.UrlReferrer.ToString())) {

                        //user not loged try to login him 
                        string myUserName = Settings["AutoLogin_UserName"].ToString();
                        string myPassword = Settings["AutoLogin_Password"].ToString();

                        DotNetNuke.Security.Membership.UserLoginStatus loginStatus = new DotNetNuke.Security.Membership.UserLoginStatus();
                    
                        DotNetNuke.Entities.Users.UserInfo userInfo = DotNetNuke.Entities.Users.UserController.ValidateUser(this.PortalId, myUserName, myPassword, "", this.PortalSettings.PortalName, Request.UserHostAddress, ref loginStatus);

                        if (userInfo != null && loginStatus == DotNetNuke.Security.Membership.UserLoginStatus.LOGIN_SUCCESS) {
                            DotNetNuke.Entities.Users.UserController.UserLogin(this.PortalId, userInfo, this.PortalSettings.PortalName, Request.UserHostAddress, true);
                            Response.Redirect(Globals.NavigateURL(this.TabId, ""));   //refresh site
                        } else 
                            DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, "Error: " +((DotNetNuke.Security.Membership.UserLoginStatus)loginStatus).ToString() +" for user: " + myUserName, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
                    
                    
                }
                if (!this.UserInfo.IsSuperUser) {
                    lblHostInfo.Visible = false;
                }
            } catch (Exception ex) {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }
    }

}
